/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       adv_dwelling.h

	$Header: /game/adv_dwelling.h $

	$NoKeywords: $

 ************************************************************************/

#if !defined( ADV_DWELLING_H_INCLUDED )
#define ADV_DWELLING_H_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "bool_array.h"
#include "creature_array.h"
#include "creature_type.h"
#include "linkage_data.h"
#include "owned_scriptable_adv_object.h"
#include "player_color.h"

class t_window;

// -------------------------------------------------------------------
// A creature dwelling on the adventure map
// -------------------------------------------------------------------
class t_adv_dwelling : public t_owned_scriptable_adv_object, public t_creature_array
{
public:
	enum { k_population_divisor = 14 }; // m_population & get_fractional_population() is divided by this to get whole creatures

	t_adv_dwelling(  std::string const& source, t_qualified_adv_object_type const& type );
	t_adv_dwelling( t_creature_type type, t_player_color owner_color = k_player_gray );

	virtual void			activate_trigger( t_army* army, t_adv_map_point const& point, 
							                  t_direction direction, t_adventure_frame* frame );
	virtual float			ai_value( t_adventure_ai const& ai, t_creature_array const& army, int move_cost ) const;
	virtual float			ai_activation_value_drop( t_creature_array const& army ) const;
	virtual void			destroy();
	virtual t_skill_mastery	get_anti_stealth_level() const;
	t_creature_type			get_creature_type() const;
	int						get_fractional_population() const;
	int						get_population() const; 
	virtual int				get_version() const;
	virtual void			initialize( t_adventure_map& map );
	virtual bool			is_triggered_by( t_creature_array const& army ) const;
	virtual void			on_combat_end( t_army* army, t_combat_result result, 
										   t_creature_array* losses, t_adventure_frame* frame );
	virtual void			place( t_adventure_map& map, t_adv_map_point const& point );
	virtual bool			preplacement( t_adventure_map& map, int pass );
	virtual void			process_new_day();
	virtual bool			read( std::streambuf&						stream, 
							      t_qualified_adv_object_type const&	type,
							      int									version );
	virtual bool			read_from_map( std::streambuf& buffer, t_progress_handler* );
	virtual void			right_click( t_mouse_event const& event,
								         t_adventure_frame* adventure_frame );
	void					run_recruit_dialog( t_window * dialog_owner, t_creature_array & destination );
	virtual void			set_owner( int player_number );
	void					set_fractional_population( int population );
	virtual bool			write( std::streambuf& stream ) const;
protected:
	void grow( int amount );
	void set_initial_values( t_creature_type creature );
	void visit( t_army* army, t_adventure_frame* frame );

	int			 m_population; // Fractional; actual creatures are m_population / k_population_divisor
	t_bool_array m_visited;
};

inline t_creature_type t_adv_dwelling::get_creature_type() const
{
	return t_creature_type( get_major_subtype() );
}

// -------------------------------------------------------------------
// Creatures in dwelling * k_population_divisor
// -------------------------------------------------------------------
inline int t_adv_dwelling::get_fractional_population() const
{
	return m_population;
}

// -------------------------------------------------------------------
// Actual # of creatures in dwelling
// -------------------------------------------------------------------
inline int t_adv_dwelling::get_population() const
{
	return m_population / k_population_divisor;
}

// -------------------------------------------------------------------
// Set the actual # of creatures in dwelling
// -------------------------------------------------------------------
inline void t_adv_dwelling::set_fractional_population( int population )
{
	m_population = population;
}

// -------------------------------------------------------------------
// A random creature dwelling on the adventure map
// -------------------------------------------------------------------
class t_random_adv_dwelling : public t_owned_scriptable_adv_object
{
public:
	t_random_adv_dwelling( std::string const& model_name );

	int          get_creature_level() const;
	virtual bool read_from_map( std::streambuf& buffer, t_progress_handler* );
	virtual void place( t_adventure_map& map, t_adv_map_point const& point );
	virtual bool preplacement( t_adventure_map& map, int pass );

protected:
	t_creature_type m_creature_type; // Chosen in preplacement()
	t_linkage_data	m_linkage_info;
};

inline int t_random_adv_dwelling::get_creature_level() const
{
	return get_major_subtype() + 1;
}

#endif // !defined( ADV_DWELLING_H_INCLUDED )
